<?php
namespace app\chance\controller;

use app\chance\model\ChanceModel;
use app\common\base\BasicRest;
use think\Request;

class Chance extends BasicRest
{
    #获取概率信息
    public function search(){
        $data = ChanceModel::all();
        $data[0]['jx'] = '一等奖';
        $data[0]['chance'] = $data[0]['probability'].'‰';
        $data[1]['jx'] = '二等奖';
        $data[1]['chance'] = $data[1]['probability'].'‰';
        $data[2]['jx'] = '三等奖';
        $data[2]['chance'] = $data[2]['probability'].'‰';
        $data[3]['jx'] = '未中奖';
        $data[3]['chance'] = (1000-$data[0]['probability']-$data[1]['probability']-$data[2]['probability']).'‰';
        if($data) return res_success($data);
        return res_fail('未找到信息');
    }

    #修改概率信息
    public function updateChance(Request $request){
        $data = $request->post();
        if($data['password']!='123') return res_fail('密码不正确');
        ChanceModel::update(['id' => 1, 'probability' => $data['chance1']]);
        ChanceModel::update(['id' => 2, 'probability' => $data['chance2']]);
        $res = ChanceModel::update(['id' => 3, 'probability' => $data['chance3']]);
        if($res) return res_success();
        return res_fail('修改失败');
    }
}